#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <iostream>
#include <stdint.h>

using namespace std;

class ServerUDP {
private:

public:
    ServerUDP(int port, char *address);

    ServerUDP(int port, char *address, bool multi);

    ServerUDP();

    ~ServerUDP();

    void setip_add(int port, char *address);

    int sendData(char *send_buf, int size);

    int receiveData(char *receive_buf, int size);

    int receiveData_nozs(char *receive_buf, int size);

    int receiveData_multi(char *receive_buf, int size);

    void printData(char *buf, int size);

    int serv_port;
    char *serv_ip_address;
    int sock_fd;

    /* 将套接字和IP、端口绑定 */
    struct sockaddr_in addr_serv;
    int len;
    struct ip_mreq mreq;
    int recv_num;
    int send_num;
    char recv_buff[1024];
    struct sockaddr_in addr_client;

    void setip_add(int port, unsigned char *address);

    int sendData(unsigned char *send_buf, int size);

    int receiveData(unsigned char *receive_buf, int size);

    int receiveData_nozs(unsigned char *receive_buf, int size);

    void printData(unsigned char *buf, int size);

    unsigned char unchar_recv_buf[1024];
    unsigned char *un_serv_ip_address;
};

class ClientUDP
{
private:
    /* data */
public:
    ClientUDP(int port, char * address);
    ClientUDP();
    ~ClientUDP();

    void setip_add(int port, char * address);
    void setip_add_multi(int port, char * address);
    int sendData(char * send_buf, int size);
    int receiveData(char * receive_buf, int size);
    int receiveData_nozs(char * receive_buf, int size);
    void printData(char * buf, int size);

    int dest_port;
    char * dest_ip_address;

    /* socket 文件描述符 */
    int sock_fd;

    /* 设置地址 */
    struct sockaddr_in addr_serv, addr_client;
    int len;
    int len_client;
    int send_num;
    int recv_num;

    void setip_add(int port, unsigned char * address);
    int sendData(unsigned char * send_buf, int size);
    int receiveData(unsigned char * receive_buf, int size);
    int receiveData_nozs(unsigned char * receive_buf, int size);
    void printData(unsigned char * buf, int size);
    unsigned char * un_dest_ip_address;
};

